package easymall.service;

import easymall.dao.CategoryDao;
import easymall.po.Category;
import easymall.pojo.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<Category> getCategorysByPage(Integer page) {
        MyPage mypage = new MyPage(page);
        return categoryDao.getCategorysByPage(mypage);
    }

    @Override
    public List<Category> allcategorys() {
        List<Category> categorys = categoryDao.allcategorys();
        return categorys;
    }

    @Override
    public int getCategorysCount() {
        return 0;
    }

    @Override
    public int checkCategorysExist(String name) {
        return 0;
    }


    @Override
    public int delCategory(Integer id) {
        return categoryDao.delCategory(id);
    }

    @Override
    public int updateCategory(Category category) {
        return categoryDao.updateCategory(category);
    }

    // 存在返回-1 保存失败返回0 成功返回1
    @Override
    public int saveCategory(String name) {
        int num = categoryDao.checkCategorysExist(name);
        if (num > 0) { // 已经存在
            return -1;
        } else {
            return categoryDao.saveCategory(name);
        }
    }

    @Override
    public int getTotalPage() {
        return (categoryDao.getCategorysCount() - 1) / MyPage.size + 1;
    }
}
